<template>
  <a-form
    :model="fromsera"
    :style="{ marginBottom: '20px' }"
    label-align="left"
    layout="inline"
    auto-label-width
    @submit="dosearch"
  >
    <a-form-item field="appName" label="应用名称">
      <a-input
        allow-clear
        v-model="fromsera.appName"
        placeholder="请输入应用名称"
      />
    </a-form-item>

    <a-form-item>
      <a-button type="primary" html-type="submit" style="width: 100px"
        >搜索
      </a-button>
    </a-form-item>
  </a-form>

  <a-table
    :columns="columns"
    :data="dataList"
    :pagination="{
      showTotal: true,
      pageSize: sera.pageSize,
      current: sera.current,
      total,
    }"
    @page-change="OnpageChang"
  >
    <template #appIcon="{ record }">
      <a-image width="64" :src="record.appIcon" />
    </template>
    <template #appType="{ record }">
      {{ APP_TYPE_MAP[record.appType] }}
    </template>
    <template #scoringStrategy="{ record }">
      {{ APP_SCORING_STRATEGY_MAP[record.scoringStrategy] }}
    </template>
    <template #reviewStatus="{ record }">
      {{ REVIEW_STATUS_MAP[record.reviewStatus] }}
    </template>
    <template #reviewTime="{ record }">
      {{
        record.reviewTime &&
        dayjs(record.reviewTime).format("YYYY-MM-DD HH:ss:mm")
      }}
    </template>
    <template #createTime="{ record }">
      {{ dayjs(record.createTime).format("YYYY-MM-DD HH:ss:mm") }}
    </template>
    <template #updateTime="{ record }">
      {{ dayjs(record.updateTime).format("YYYY-MM-DD HH:ss:mm") }}
    </template>
    <template #optional="{ record }">
      <a-space>
        <a-button
          v-if="record.reviewStatus !== REVIEW_STATUS_ENUM.PASS"
          status="success"
          @click="doReview(record, REVIEW_STATUS_ENUM.PASS, '')"
        >
          通过
        </a-button>
        <a-button
          v-if="record.reviewStatus !== REVIEW_STATUS_ENUM.REJECT"
          status="warning"
          @click="doReview(record, REVIEW_STATUS_ENUM.REJECT, '不符合上架要求')"
        >
          拒绝
        </a-button>
        <a-button status="danger" @click="doDelete(record)">删除</a-button>
      </a-space>
    </template>
  </a-table>
</template>

<script setup lang="ts">
import { ref, watchEffect } from "vue";

import API from "@/api";
import meassage from "@arco-design/web-vue/es/message";
import { dayjs } from "@arco-design/web-vue/es/_utils/date";
import {
  deleteAppUsingPost,
  listAppByPageUsingPost,
  reviewAppUsingPost,
} from "@/api/appController";
import {
  APP_SCORING_STRATEGY_MAP,
  APP_TYPE_MAP,
  REVIEW_STATUS_ENUM,
  REVIEW_STATUS_MAP,
} from "../../../constant/app";

//删除
const doDelete = async (record: API.App) => {
  if (!record.id) {
    return;
  }
  const res = await deleteAppUsingPost({
    id: record.id,
  });
  if (res.data.code === 0) {
    lodDate();
    meassage.success("删除成功");
  } else {
    meassage.error("删除失败");
  }
};

const fromsera = ref<API.AppQueryRequest>({});

//初始化搜索条件
const initsera = {
  current: 1,
  pageSize: 10,
};

const sera = ref<API.AppQueryRequest>({
  ...initsera,
});
const dataList = ref<API.App[]>([]);
const total = ref<number>();

/**
 * 审核
 */
const doReview = async (
  record: API.App,
  reviewStatus: number,
  reviewMessage?: string
) => {
  if (!record.id) {
    return;
  }

  const res = await reviewAppUsingPost({
    id: record.id,
    reviewStatus,
    reviewMessage,
  });
  if (res.data.code === 0) {
    lodDate();
  } else {
    meassage.error("审核失败，" + res.data.message);
  }
};

/**
 * 当分页变化时，改变搜索条件，触发数据加载
 * @param page
 * @constructor
 */
const OnpageChang = (page: number) => {
  sera.value = {
    ...sera.value,
    current: page,
  };
};
/**
 * 加载数据
 */
const lodDate = async () => {
  const res = await listAppByPageUsingPost(sera.value);
  if (res.data.code === 0) {
    dataList.value = res.data.data?.records || [];
    total.value = res.data.data?.total || 0;
  } else {
    meassage.error("获取数据失败" + res.data.message);
  }
};

/**
 * 执行搜索
 */
const dosearch = () => {
  sera.value = {
    ...initsera,
    ...fromsera.value,
  };
};
/**
 * 数据发生改变时进行监听
 */
watchEffect(() => {
  lodDate();
});

const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "应用名",
    dataIndex: "appName",
  },
  {
    title: "描述",
    dataIndex: "appDesc",
  },

  {
    title: "图标",
    dataIndex: "appIcon",
    slotName: "appIcon",
  },
  {
    title: "应用类型",
    dataIndex: "appType",
    slotName: "appType",
  },
  {
    title: "评分策略",
    dataIndex: "scoringStrategy",
    slotName: "scoringStrategy",
  },
  {
    title: "审核状态",
    dataIndex: "reviewStatus",
    slotName: "reviewStatus",
  },
  {
    title: "审核信息",
    dataIndex: "reviewMessage",
  },
  {
    title: "审核时间",
    dataIndex: "reviewTime",
    slotName: "reviewTime",
  },
  {
    title: "审核人ID",
    dataIndex: "reviewerId",
  },
  {
    title: "用户ID",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    slotName: "createTime",
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    slotName: "updateTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
</script>
